package com.eportal.util;


import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.awt.Color;  
import java.awt.Font;  
import java.awt.Graphics2D;  
import java.awt.image.BufferedImage;  
import java.io.File;  
import java.io.IOException;  
import java.net.URL;
import java.util.HashMap;
import java.util.Map;  

public class ImageUtil {

	private static Logger log = Logger.getLogger(ImageUtil.class);
	
	 private static Font font = new Font("宋体", Font.BOLD, 40);// 添加字体的属性设置  
	  
	  
	    private int fontsize = 0;  
	  
	  
	    /** 
	     * 导入本地图片到缓冲区 
	     */  
	    public static BufferedImage loadImageLocal(String imgName) { 
	        try {  
	            return ImageIO.read(new File(imgName));  
	        } catch (IOException e) {  
	        	log.error("loadImageLocal error",e);
	        }  
	        return null;  
	    }  
	  
	    /** 
	     * 导入网络图片到缓冲区 
	     */  
	    public static BufferedImage loadImageUrl(String imgName) {  
	        try {  
	            URL url = new URL(imgName);  
	            return ImageIO.read(url);  
	        } catch (IOException e) {  
	        	log.error("loadImageLocal error",e);
	        }  
	        return null;  
	    }  
	  
	    /** 
	     * 生成新图片到本地 
	     * @throws IOException 
	     */  
	    public static void writeImageLocal(String newImage, BufferedImage img) throws IOException {  
	        if (newImage != null && img != null) {  
                File outputfile = new File(newImage);  
                ImageIO.write(img, "bmp", outputfile);  
	        }  
	    }  
	  
	    /** 
	     * 设定文字的字体等 
	     */  
	    public void setFont(String fontStyle, int fontSize) {  
	        this.fontsize = fontSize;  
	        this.font = new Font(fontStyle, Font.PLAIN, fontSize);  
	    }  
	  
	    /** 
	     * 修改图片,返回修改后的图片缓冲区（只输出一行文本） 
	     */  
	    public static BufferedImage modifyImage(BufferedImage img, Object content, int x,  
	            int y,Font font) {  
	  
	        try {  
	        	int fontsize=0;
	        	if(font!=null){
	        		fontsize=font.getSize();
	        	}
	            int w = img.getWidth();  
	            int h = img.getHeight();  
	            Graphics2D  g = img.createGraphics();  
	            g.setBackground(Color.WHITE);  
	            g.setColor(Color.black);//设置字体颜色  
	            
	            int x_=0,y_=0;
	            
	            if (font != null){
	            	g.setFont(font);
	            }
	                  
	            // 验证输出位置的纵坐标和横坐标  
	           /* if (x >= h || y >= w) {  
	            	x_ = h - fontsize + 2;  
	                y_ = w;  
	            } else {  */
	                x_ = x;  
	                y_ = y;  
	            /*}  */
	            if (content != null) {  
	                g.drawString(content.toString(), x_,y_);  
	            }  
	            g.dispose();  
	        } catch (Exception e) {  
	        	log.error("error",e);
	        }  
	  
	        return img;  
	    }  
	
	  
	    public static BufferedImage modifyImagetogeter(BufferedImage child, BufferedImage parent,int x,int y) {  
	  
	        try {  
	            int w = child.getWidth();  
	            int h = child.getHeight();  
	            Graphics2D g = parent.createGraphics();  
	            g.drawImage(child, x, y, w, h, null);  
	            g.dispose();  
	            return parent;
	        } catch (Exception e) {  
	            log.error("", e);
	        }  
	        return null;  
	    }  
	    
		public static BufferedImage toBufferedImage(BitMatrix matrix) {
			int width = matrix.getWidth();
			int height = matrix.getHeight();
			BufferedImage image = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			for (int x = 0; x < width; x++) {
				for (int y = 0; y < height; y++) {
					image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
				}
			}
			return image;
		}
		
		private static final int BLACK = 0xFF000000;
		private static final int WHITE = 0xFFFFFFFF;
	  
	    public static void main(String[] args) throws WriterException, IOException {  
	  
	    	ImageUtil tt = new ImageUtil();  
	  
	        BufferedImage d = tt.loadImageLocal("F:\\lablekzq.bmp");  
	        Font font = new Font("宋体", Font.BOLD, 40);
	        tt.modifyImage(d,"BLZXA160300009",100,285,font);
	         //tt.writeImageLocal("F:\\t1.bmp",tt.modifyImage(d,"BLZXA160300009",100,285)   );  
	         
	        //往图片上写文件  
			Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
			hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//编码
			hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);//容错率
			hints.put(EncodeHintType.MARGIN, 0);
	      // 生成二维码
				BitMatrix bitMatrix = new MultiFormatWriter().encode("BLZXA160300009",
						BarcodeFormat.QR_CODE, 150, 150, hints);
				// 输出图片
				
				BufferedImage qrImage = toBufferedImage(bitMatrix);
	         
	         
	         
	        tt.writeImageLocal("F:\\t2.bmp", tt.modifyImagetogeter(qrImage, d,20,20));  
	        //将多张图片合在一起  
	        System.out.println("success");  
	        
	    }  
	    
	    

}
